package com.tsky.yyj.manager.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.springframework.stereotype.Repository;

import com.tsky.casing.QueryBean;
import com.tsky.casing.orm.impl.EntityManagerImpl;
import com.tsky.common.BeanUtil;
import com.tsky.yyj.manager.BaseManager;

@Repository("baseManager")
public class BaseManagerImpl extends EntityManagerImpl implements BaseManager {

	public void query(QueryBean queryBean, Class classType) {
		// TODO Auto-generated method stub
		// 基本查询条件
		int page = queryBean.getPage();
		int pageSize = queryBean.getRows(); // page size
		String orderField = queryBean.getSort();
		String sortOrder = queryBean.getOrder();
		
		Criteria ctr = this.getSession().createCriteria(classType);

		// 组合查询

		// 排序
		if (!BeanUtil.isEmpty(sortOrder)) {
			if ("asc".equals(sortOrder)) {
				ctr.addOrder(Order.asc(orderField));
			} else {
				ctr.addOrder(Order.desc(orderField));
			}
		}
		
		// 总记录数
		List<?> resultList = ctr.list();
		queryBean.setTotal(resultList.size());

		ctr.setFirstResult(pageSize * (page - 1));
		ctr.setMaxResults(pageSize);
		
		queryBean.setDataList(ctr.list());
	}

}
